<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Menus;
use Illuminate\Support\Facades\Redis;

class CategoryController extends Controller
{
    protected $menus;
    public function __construct()
    {
        $this->menus = new Menus();
    }

    //
    function index(){
        $data['menus'] = $this->menus->all();
        return view('Admin.menu',$data);
    }

    /**
     * 加载菜单数据结构
     * @return string
     */
    function  menuJosnData(){
        $cacheKey = 'page:admin:menu';
        $pageCache = Redis::get($cacheKey);
        if(!$pageCache) {
            $parent =  $this->menus
                ->where('parent_id','=',0)
                ->get(['id','name','route','isActive','icon'])
                ->toArray();
            foreach ($parent as $key=>$value){
                $childList =  $this->menus
                    ->where('parent_id','=',$value['id'])
                    ->get(['id','name','route','isActive','icon'])
                    ->toArray();
                if(count($childList)>0){
                    foreach ($childList as $item){
                        $parent[$key]['subItems'][] = $item;
                    }
                }
            }
            $json  = json_encode($parent);
            Redis::set($cacheKey, $json);
            return $json;
        }
        return $pageCache;
    }
}


